package com.doublecheck;

import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.tokenizer.StandardTokenizer;
import org.apache.commons.lang3.StringUtils;

public class WordGeter {
    public Map<String, String> stopNatures = new HashMap<String, String>();// 停用词的词性
    public Map<String, Integer> weightOfNature = new HashMap<String, Integer>();// 词性的权重
    public int maxTimes; // 某个词最大出现频率
    public Map<String, Integer> weight = new HashMap<String, Integer>();// 某个词的出现次数
    public String inFilePath;// 待查重的文件路径

    public WordGeter() {
        this.inFilePath = "";
    }

    public WordGeter(String inFilePath) {
        this.inFilePath = inFilePath;
    }

    public void settings() {
        // 词性权重，超频词等设置
        this.maxTimes = 5;
        // 配置停用词性为w:标点
        this.stopNatures.put("w", "");
        // 个性化设置词性权重，这里将n：名词设置为2，v：动词设置为1。（默认权重为1）
        this.weightOfNature.put("n", 3);
        this.weightOfNature.put("v", 2);
    }

    public void getwords() throws IOException {
        // 将一篇文本进行分词
        settings();
        if (this.inFilePath.equals(""))
            this.getInFilePath();
        String sentences = this.getsentences(this.inFilePath);
        this.separeteWords(sentences);
        return;
    }

    public void getInFilePath() {
        // 通过文件选择器获取待查重文件的路径
        FileNameExtensionFilter filter = new FileNameExtensionFilter("*.txt", "txt");
        JFileChooser file = new JFileChooser();
        file.setFileFilter(filter);
        file.setAcceptAllFileFilterUsed(false);
        int result = file.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            String path = file.getSelectedFile().getAbsolutePath();
            this.inFilePath = path;
        }
        return;
    }

    public String getsentences(String inFilePath) throws IOException {
        // 从文件中读取句子，并且粗加工(统一大小写，去掉特殊字符)
        boolean isUseless;
        char[] uselessChars = { ' ', '\n', '\r', '\t' };
        String sentences = new String();// 从文件中读取的句子
        char current;
        int currentInt;
        InputStreamReader input = new InputStreamReader(new FileInputStream(inFilePath), "UTF-8");
        while ((currentInt = input.read()) != -1) {
            isUseless = false;
            current = (char) currentInt;
            for (char endLetter : uselessChars) {
                if (current == endLetter) {
                    isUseless = true;
                    break;
                }
            }
            if (!isUseless)
                sentences = sentences + current;
        }
        sentences = StringUtils.lowerCase(sentences);
        input.close();
        return sentences;
    }

    public void separeteWords(String sentences) {
        // 对文本经行分词，提取信息
        List<Term> termList = StandardTokenizer.segment(sentences);
        for (Term term : termList) {
            String word = term.word;
            String nature = term.nature.toString();
            if (!this.stopNatures.containsKey(nature)) {
                Integer oldweight = this.weight.get(word);
                Integer natureweight=this.weightOfNature.get(word);
                if(natureweight==null) natureweight=1;
                if (oldweight!=null) {
                    if (oldweight <= this.maxTimes*natureweight)
                        this.weight.put(word, oldweight + natureweight);
                } else
                    this.weight.put(word, natureweight);
            }
        }
        return;
    }
}